const axios = require('axios')
const qrCreate = require("qr-image")

const fs = require('fs')
 
async function fetchHTML(url) {
  try {
    const { data } = await axios.get(url)
    return data
  } catch (error) {
    console.error('Error fetching HTML:', error)
  }
}

const getItems = async () => {
    // const resp = await axios.get('https://buff.163.com/api/market/goods/sell_order?game=dota2&goods_id=953546&page_num=1&sort_by=default&mode=&allow_tradable_cooldown=1&_=1736436769376')
    // console.log(resp.data.data)
    // console.log(resp.data.data.items[0])
    // const item = JSON.stringify(resp.data.data.items[0].price)
    // console.log(item)
    // fs.writeFile('output.txt', JSON.stringify(resp.data.data), (err) => {
    //     if (err) throw err;
    //     console.log('文件已被保存');
    //   })
  let url = 'https://buff.163.com/goods/953546?from=market#tab=selling'
  let qr_png = qrCreate.image(url,{type:"png"})
  // 创建可以写入流，当有pipe它的时候就会生成一个userStr.png的文件
  let img = fs.createWriteStream("url.png")
  qr_png.pipe(img)
}
 
async function crawlPage(url) {
  try {
    const html = await fetchHTML(url)
    if (html) {
      const $ = cheerio.load(html)
      // 使用cheerio的选择器和方法来操作HTML，例如获取某个元素的文本内容
      const content = $('#market-selling-list')
      console.log(content.text())
      fs.writeFile('output.html', html, (err) => {
        if (err) throw err;
        console.log('文件已被保存');
      })
    }
  } catch (error) {
    console.error('Error crawling page:', error)
  }
}
 
const url = 'https://buff.163.com/goods/953546#tab=selling'
// crawlPage(url)

getItems()